package com.hgsf.controller;

import com.hgsf.entity.*;
import com.hgsf.service.AdminService;
import com.hgsf.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/student")
@Controller
public class StudentController {
    @Autowired
    private StudentService studentService;
    @Autowired
    private AdminService adminService;
    @Autowired
    private HttpServletRequest request;
    @ResponseBody
    @RequestMapping("/login")
    public String login(Student student, String checkcode){
        Student stu = null;
        String s = "";
        HttpSession session = request.getSession();
        if (session.getAttribute("checkcode").equals(checkcode)) {

            try {
                stu = studentService.studentLogin(student);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (stu != null) {
                s="success";
                session.setAttribute("status","student");
                session.setAttribute("name",stu.getSname());
                session.setAttribute("sid",stu.getSid());
            } else {
                session.setAttribute("status","psw_wrong");
                s="failed";
            }
        }
        else {
            session.setAttribute("status","verify_wrong");
            s="failed";
        }
        return s;
    }

    @RequestMapping("/list_option_course")
    public ModelAndView listCourse() {
        ModelAndView modelAndView = new ModelAndView();
        Object o = request.getSession().getAttribute("sid");
        if(o==null){
            return new ModelAndView("loginin");
        }
        List<Map<String,String>> datas = studentService.queryOptionCourse((long)o);
        List<Teacher> teachers = adminService.queryTeacher();
        List<Major> majors = adminService.queryMajor();
        modelAndView.addObject("datas", datas);
        modelAndView.addObject("teachers", teachers);
        modelAndView.addObject("majors", majors);
        modelAndView.setViewName("view/list_option_course");
        return modelAndView;
    }
    @ResponseBody
    @RequestMapping("select_course")
    public String selectCourse(SelectedCourse sc) {
        System.out.println(sc.toString());
        Object o = request.getSession().getAttribute("sid");
        if(o==null){
            return "error";
        }
        sc.setSid((long)o);
        System.out.println(sc.toString());
        boolean b = false;
        try {
            b = studentService.selectCourse(sc);
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (b) {
            return "success";
        }
        return "failed";
    }


    @RequestMapping("list_sedc")
    public ModelAndView listSelectedCourse(){
        Object o = request.getSession().getAttribute("sid");
        if(o==null){
            return new ModelAndView("loginin");
        }
        long sid = (long) o;
        List<HashMap<String,String>> datas = studentService.querySelectedCourse(sid);
        ModelAndView modelAndView = new ModelAndView("view/stu_sc");
        modelAndView.addObject("datas",datas);
        return modelAndView;
    }
}
